package chances.epg.data.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Repository;

import chances.epg.data.PageBean;
import chances.epg.data.ScheduleVO;
import chances.epg.entity.content.EpgSchedule;
import chances.epg.entity.content.EpgScheduleNav;
import chances.epg.entity.content.ScheduleTimer;


/**
 * 
 * @author chances
 *
 */
@Repository
@EnableScheduling
public class ScheduleService extends EpgService  {
	private static Map<String, List<EpgSchedule>> nowSchedule;
	private int loadTime = 180;
	
	@PostConstruct
	public void init() {
		ScheduleService.nowSchedule = this.listSchedules(loadTime, loadTime);
	}
	
	/**
	 * 查询一段时间的所有节目单
	 * @param after 多少分钟后
	 * @param before 多少分钟前
	 * @return 根据每屏道区分的节目单
	 */
	@SuppressWarnings("unchecked")
	public Map<String, List<EpgSchedule>> listSchedules( int before,int after) {
		Map<String, List<EpgSchedule>> result = new HashMap<String, List<EpgSchedule>>();
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		Calendar afterDate = Calendar.getInstance();
		afterDate.add(Calendar.MINUTE, after);
		Calendar beforeData = Calendar.getInstance();
		beforeData.add(Calendar.MINUTE, -before);
		paramsMap.put("startTime", beforeData.getTime());
		paramsMap.put("endTime", afterDate.getTime());
		
		List<EpgSchedule> schedules = (List<EpgSchedule>)this.queryForList("listSchedule", paramsMap);
		if(schedules !=null){
			for (EpgSchedule sche : schedules) {
				List<EpgSchedule> channelSchedules = result.get(sche.getChannelCode());
				if (channelSchedules == null) {
					channelSchedules = new ArrayList<EpgSchedule>();
					result.put(sche.getChannelCode(), channelSchedules);
				}
				channelSchedules.add(sche);
			}
		}
		return result;
	}

	@Scheduled(fixedDelay = 180000)
	public void loadSchedule() {
		init();
	}
	
	
	/**
	 * 查询节目单
	 * @param code
	 * @return
	 */
	public EpgSchedule getScheduleByCode(String code){
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("code", code);
		return (EpgSchedule) this.queryEpgSchedule("getScheduleByCode", paramsMap);
	}
	


	/**
	 * 查询所有节目单
	 * @param channelCode
	 * @param scheduleTimer
	 * @param start
	 * @param size
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<EpgSchedule> listScheduleByChannelCode(String channelCode,ScheduleTimer scheduleTimer,int start,int size){
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("channelCode", channelCode);
		if(null != scheduleTimer){
			paramsMap.put("startTime", scheduleTimer.getStartTime());
			paramsMap.put("endTime", scheduleTimer.getEndTime());
		}
		return (List<EpgSchedule>) this.queryForList("listSchedule", paramsMap, start, size);
	}
	
	/**
	 * 查询所有节目单(翻页)
	 * @param channelCode
	 * @param scheduleTimer
	 * @param pageBean
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<EpgSchedule> listScheduleByChannelCode(String channelCode,ScheduleTimer scheduleTimer, PageBean pageBean){
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("channelCode", channelCode);
		if(null != scheduleTimer){
			paramsMap.put("startTime", scheduleTimer.getStartTime());
			paramsMap.put("endTime", scheduleTimer.getEndTime());
		}
		return (List<EpgSchedule>) this.queryForList("listSchedule", paramsMap,0, pageBean);
	}
	
	/**
	 * 查询所有节目单
	 * @param mainFolder
	 * @param tag
	 * @param scheduleTimer
	 * @param start
	 * @param size
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<EpgSchedule> listScheduleByMainFolder(String mainFolder,String tag,ScheduleTimer scheduleTimer,int start,int size){
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("mainFolder", mainFolder);
		paramsMap.put("tag", tag);
		if(null != scheduleTimer){
			paramsMap.put("startTime", scheduleTimer.getStartTime());
			paramsMap.put("endTime", scheduleTimer.getEndTime());
		}
		return (List<EpgSchedule>) this.queryForList("listSchedule", paramsMap, start, size);
	}
	
	/**
	 * 查询所有节目单(翻页)
	 * @param mainFolder
	 * @param tag
	 * @param scheduleTimer
	 * @param pageBean
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<EpgSchedule> listScheduleByMainFolder(String mainFolder,String tag,ScheduleTimer scheduleTimer, PageBean pageBean){
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("mainFolder", mainFolder);
		paramsMap.put("tag", tag);
		if(null != scheduleTimer){
			paramsMap.put("startTime", scheduleTimer.getStartTime());
			paramsMap.put("endTime", scheduleTimer.getEndTime());
		}
		return (List<EpgSchedule>) this.queryForList("listSchedule", paramsMap, 0, pageBean);
	}
	
	/**
	 * 查询所有节目单
	 * @param scheduleVO
	 * @param scheduleTimer
	 * @param start
	 * @param size
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<EpgSchedule> listSchedule(ScheduleVO scheduleVO,ScheduleTimer scheduleTimer,int start,int size){
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("channelCode", scheduleVO.getChannelCode());
		paramsMap.put("mainFolder", scheduleVO.getMainFolder());
		paramsMap.put("tag", scheduleVO.getTag());
		if(null != scheduleTimer){
			paramsMap.put("startTime", scheduleTimer.getStartTime());
			paramsMap.put("endTime", scheduleTimer.getEndTime());
		}
		return (List<EpgSchedule>) this.queryForList("listSchedule", paramsMap, start, size);
	}
	
	/**
	 *  查询所有节目单 翻页
	 * @param scheduleVO
	 * @param scheduleTimer
	 * @param pageBean
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<EpgSchedule> listSchedule(ScheduleVO scheduleVO, ScheduleTimer scheduleTimer, PageBean pageBean){
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("channelCode", scheduleVO.getChannelCode());
		paramsMap.put("mainFolder", scheduleVO.getMainFolder());
		paramsMap.put("tag", scheduleVO.getTag());
		if(null != scheduleTimer){
			paramsMap.put("startTime", scheduleTimer.getStartTime());
			paramsMap.put("endTime", scheduleTimer.getEndTime());
		}
		return (List<EpgSchedule>) this.queryForList("listSchedule", paramsMap, 0,pageBean);
	}
	
	/**
	 * 查询所有的正在播放的节目单
	 * @param channelCode
	 * @param start
	 * @param size
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<EpgSchedule> listPlayingByChannelCode(String channelCode,int start,int size){
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("channelCode", channelCode);
		return (List<EpgSchedule>) this.queryForList("listSchedulePlaying", paramsMap, start, size);
	}
	
	/**
	 * 查询所有的正在播放的节目单
	 * @param mainFolder
	 * @param tag
	 * @param start
	 * @param size
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<EpgSchedule> listPlayingByMainFolder(String mainFolder,String tag,int start,int size){
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("mainFolder", mainFolder);
		paramsMap.put("tag", tag);
		return (List<EpgSchedule>) this.queryForList("listSchedulePlaying", paramsMap, start, size);
	}
	
	/**
	 * 查询所有的正在播放的节目单(翻页)
	 * @param mainFolder
	 * @param tag
	 * @param pageBean
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<EpgSchedule> listPlayingByMainFolder(String mainFolder,String tag, PageBean pageBean){
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("mainFolder", mainFolder);
		paramsMap.put("tag", tag);
		return (List<EpgSchedule>) this.queryForList("listSchedulePlaying", paramsMap,  0,pageBean);
	}
	
	/**
	 * 查询所有的正在播放的节目单
	 * @param scheduleVO
	 * @param start
	 * @param size
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<EpgSchedule> listPlaying(ScheduleVO scheduleVO,int start,int size){
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("channelCode", scheduleVO.getChannelCode());
		paramsMap.put("mainFolder", scheduleVO.getMainFolder());
		paramsMap.put("tag", scheduleVO.getTag());
		return (List<EpgSchedule>) this.queryForList("listSchedulePlaying", paramsMap, start, size);
	}
	
	/**
	 * 查询所有的正在播放的节目单(翻页)
	 * @param scheduleVO
	 * @param pageBean
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<EpgSchedule> listPlaying(ScheduleVO scheduleVO, PageBean pageBean){
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("channelCode", scheduleVO.getChannelCode());
		paramsMap.put("mainFolder", scheduleVO.getMainFolder());
		paramsMap.put("tag", scheduleVO.getTag());
		return (List<EpgSchedule>) this.queryForList("listSchedulePlaying", paramsMap, 0, pageBean);
	}
	
	/**
	 * 查询未播放的节目单
	 * @param channelCode
	 * @param start
	 * @param size
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<EpgSchedule> listPlayNextByChannelCode(String channelCode,int start,int size){
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("channelCode", channelCode);
		return (List<EpgSchedule>) this.queryForList("listScheduleNextBySize", paramsMap, start, size);
	}
	
	/**
	 * 查询未播放的节目单(翻页)
	 * @param channelCode
	 * @param endTime
	 * @param pageBean
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<EpgSchedule> listPlayNextByChannelCode(String channelCode,Date endTime, PageBean pageBean){
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("channelCode", channelCode);
		paramsMap.put("endTime",endTime);
		return (List<EpgSchedule>) this.queryForList("listScheduleNext", paramsMap, 0, pageBean);
	}
	
	/**
	 * 查询未播放的节目单
	 * @param mainFolder
	 * @param tag
	 * @param start
	 * @param size
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<EpgSchedule> listPlayNextByMainFolder(String mainFolder,String tag,int start,int size){
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("mainFolder", mainFolder);
		paramsMap.put("tag", tag);
		return (List<EpgSchedule>) this.queryForList("listScheduleNextBySize", paramsMap, start, size);
	}
	
	/**
	 * 查询未播放的节目单（翻页）
	 * @param mainFolder
	 * @param tag
	 * @param endTime
	 * @param pageBean
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<EpgSchedule> listPlayNextByMainFolder(String mainFolder,String tag,Date endTime, PageBean pageBean){
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("mainFolder", mainFolder);
		paramsMap.put("tag", tag);
		paramsMap.put("endTime",endTime);
		return (List<EpgSchedule>) this.queryForList("listScheduleNext", paramsMap, 0, pageBean);
	}
	
	/**
	 * 查询未播放的节目单
	 * @param scheduleVO
	 * @param start
	 * @param size
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<EpgSchedule> listPlayNext(ScheduleVO scheduleVO,int start,int size){
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("channelCode", scheduleVO.getChannelCode());
		paramsMap.put("mainFolder", scheduleVO.getMainFolder());
		paramsMap.put("tag", scheduleVO.getTag());
		return (List<EpgSchedule>) this.queryForList("listScheduleNextBySize", paramsMap, start, size);
	}
	
	/**
	 * 查询未播放的节目单（翻页）
	 * @param scheduleVO
	 * @param endTime
	 * @param pageBean
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<EpgSchedule> listPlayNext(ScheduleVO scheduleVO,Date endTime, PageBean pageBean){
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("channelCode", scheduleVO.getChannelCode());
		paramsMap.put("mainFolder", scheduleVO.getMainFolder());
		paramsMap.put("tag", scheduleVO.getTag());
		paramsMap.put("endTime",endTime);
		return (List<EpgSchedule>) this.queryForList("listScheduleNext", paramsMap, 0, pageBean);
	}
	
	
	/**
	 * 查询已经播放的节目单
	 * @param channelCode
	 * @param start
	 * @param size
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<EpgSchedule> listPlayPrevByChannelCode(String channelCode,int start,int size){
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("channelCode", channelCode);
		return (List<EpgSchedule>) this.queryForList("listSchedulePrevBySize", paramsMap, start, size);
	}
	
	/**
	 * 查询已经播放的节目单(翻页)
	 * @param channelCode
	 * @param startTime
	 * @param pageBean
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<EpgSchedule> listPlayPrevByChannelCode(String channelCode,Date startTime,PageBean pageBean){
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("channelCode", channelCode);
		paramsMap.put("startTime", startTime);
		return (List<EpgSchedule>) this.queryForList("listSchedulePrev", paramsMap, 0, pageBean);
	}
	
	/**
	 * 查询已经播放的节目单
	 * @param mainFolder
	 * @param tag
	 * @param start
	 * @param size
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<EpgSchedule> listPlayPrevByMainFolder(String mainFolder,String tag,int start,int size){
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("mainFolder", mainFolder);
		paramsMap.put("tag", tag);
		return (List<EpgSchedule>) this.queryForList("listSchedulePrevBySize", paramsMap, start, size);
	}
	
	/**
	 * 查询已经播放的节目单(翻页)
	 * @param mainFolder
	 * @param tag
	 * @param startTime
	 * @param pageBean
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<EpgSchedule> listPlayPrevByMainFolder(String mainFolder,String tag,Date startTime, PageBean pageBean){
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("mainFolder", mainFolder);
		paramsMap.put("tag", tag);
		paramsMap.put("startTime", startTime);
		return (List<EpgSchedule>) this.queryForList("listSchedulePrev", paramsMap,  0,pageBean);
	}
	
	/**
	 * 查询已经播放的节目单
	 * @param scheduleVO
	 * @param start
	 * @param size
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<EpgSchedule> listPlayPrev(ScheduleVO scheduleVO,int start,int size){
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("channelCode", scheduleVO.getChannelCode());
		paramsMap.put("mainFolder", scheduleVO.getMainFolder());
		paramsMap.put("tag", scheduleVO.getTag());
		return (List<EpgSchedule>) this.queryForList("listSchedulePrevBySize", paramsMap, start, size);
	}
	
	/**
	 * 查询已经播放的节目单(翻页)
	 * @param scheduleVO
	 * @param startTime
	 * @param pageBean
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<EpgSchedule> listPlayPrev(ScheduleVO scheduleVO,Date startTime,PageBean pageBean){
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("channelCode", scheduleVO.getChannelCode());
		paramsMap.put("mainFolder", scheduleVO.getMainFolder());
		paramsMap.put("tag", scheduleVO.getTag());
		paramsMap.put("startTime", startTime);
		return (List<EpgSchedule>) this.queryForList("listSchedulePrev", paramsMap, 0, pageBean);
	}
	

	/**
	 * 通过channelCode获取EpgScheduleNav
	 * @param channelCode
	 * @return
	 */
	public EpgScheduleNav getScheduleNavByCode(String channelCode){
		EpgScheduleNav scheduleNav = new EpgScheduleNav();
		Date now = new Date();
		List<EpgSchedule> schedules = nowSchedule.get(channelCode);
		List<EpgSchedule> prevSchedules = new ArrayList<EpgSchedule>();
		List<EpgSchedule> nextSchedules = new ArrayList<EpgSchedule>();
		if(schedules == null){
			return null;
		}
		int i = 0;
		for(Iterator<EpgSchedule> iterator = schedules.iterator();iterator.hasNext();i++){
			EpgSchedule schedule = iterator.next();
			if(schedule.getEndTime().getTime() <= now.getTime()){
				prevSchedules.add(schedule);
			}else if(schedule.getStartTime().after(now)){
				nextSchedules.add(schedule);
			}else {
				scheduleNav.setSchedule(schedule);
				if(i > 1){
					scheduleNav.setPrevSchedule(schedules.get(i-1));
				}
				if(i < schedules.size()-1){
					scheduleNav.setNextSchedule(schedules.get(i+1));
				}
			}
		}
		return scheduleNav;
	}
	
	@SuppressWarnings("unchecked")
	private EpgSchedule queryEpgSchedule(String string, Map<String, Object> paramsMap) {
		List<EpgSchedule> result = (List<EpgSchedule>) this.queryForList(string, paramsMap);
		if(result.size()>0){
			return result.get(0);
		}else{
			return null;
		}
	}
}

